import pygame
import random
import sys
import time
import _thread
from pygame.locals import *
fontname="AlibabaFont.otf"
font = lambda s, w, c: pygame.font.Font("AlibabaFont.otf", s).render(w, True, c)
def getk():
    return random.choice([0]*50+[1]*40+[2]*10)
def getm(n,w):
    lis=[]
    for i in range(n):
        lis2=[]
        for j in range(w):
            lis2.append(getk())
        lis.append(lis2)
    return lis
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode((500,600))
pygame.display.set_caption("矿工游戏")
kmap=[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,1,0,0]]+getm(310,5)
slide=0
man=pygame.transform.scale(pygame.image.load("man.svg"),(100,100))
pygame.display.set_icon(man)
mancur=2
manslide=2
gameover=False
score=0
def updslide():
    global slide,manslide,score,gameover
    while True:
        time.sleep(0.5)
        if not gameover:
            slide+=1
        if score>=300:
            gameover=True
ddelay=0.3
def down():
    global manslide,score,ddelay
    while True:
        time.sleep(ddelay)
        if not gameover:
            if kmap[manslide+1][mancur]==0:
                manslide+=1
                score+=1
                ddelay=1/(1/ddelay+1/0.3)
            else:
                 ddelay=0.3
_thread.start_new_thread(updslide,())
_thread.start_new_thread(down,())
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if not gameover:
                if event.key==K_LEFT and mancur!=0:
                    if kmap[manslide+1][mancur]!=0:
                        mancur-=1
                        if kmap[manslide][mancur]==2:
                            kmap[manslide][mancur]=0
                            score+=10
                        if kmap[manslide][mancur]==1:
                            kmap[manslide][mancur]=0
                    elif kmap[manslide][mancur-1]==0:
                        mancur-=1
                if event.key==K_RIGHT and mancur!=4:
                    if kmap[manslide+1][mancur]!=0:
                        mancur+=1
                        if kmap[manslide][mancur]==2:
                            kmap[manslide][mancur]=0
                            score+=10
                        if kmap[manslide][mancur]==1:
                            kmap[manslide][mancur]=0
                    elif kmap[manslide][mancur+1]==0:
                        mancur+=1
                if event.key==K_DOWN:
                    if kmap[manslide+1][mancur]==2:
                        score+=10
                    if kmap[manslide+1][mancur]==0:
                        manslide+=1
                    else:
                        kmap[manslide+1][mancur]=0
    if manslide<slide or manslide>=slide+4:
        gameover=True
    screen.fill("lightgray")
    for i in range(slide,slide+5):
        for j in range(5):
            if kmap[i][j]==1:
                pygame.draw.rect(screen,"black",pygame.Rect(j*100+5,(i-slide)*100+5,90,90))
            if kmap[i][j]==2:
                pygame.draw.rect(screen,"gold",pygame.Rect(j*100+5,(i-slide)*100+5,90,90))
    screen.blit(man,(mancur*100+5,(manslide-slide)*100+5))
    if gameover:
        screen.fill("lightgray")
        go=font(48,"GAME OVER","green")
        tip=font(24,"Restart the program to play again","blue")
        gor=go.get_rect()
        tipr=tip.get_rect()
        gor.top=200
        gor.centerx=250
        tipr.top=300
        tipr.centerx=250
        screen.blit(go,gor)
        screen.blit(tip,tipr)
    sc=font(24,"Score: {}".format(score),"black")
    screen.blit(sc,(50,500))
    pygame.display.flip()
